//
//  MyShopListController.m
//  carowner
//
//  Created by apple on 2019/5/21.
//  Copyright © 2019 ggj. All rights reserved.
//

#import "MyShopListController.h"
#import "ServiceShopCell.h"
#import "ShopListEntity.h"
#import "ShopDetailsViewController.h"
#import "CleanOrderController.h"

@interface MyShopListController ()<OnItemClick>

@property(nonatomic,strong)GetShopParams * shopParams;

@property(nonatomic,strong)UIActivityIndicatorView * uiView;

@end

@implementation MyShopListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)initData{
    _uiView = [NSObject getIndicatorView];
    _shopParams = [[GetShopParams alloc]init];
    _shopParams.longitude = @"119.43";
    _shopParams.latitude = @"32.38";
    _shopParams.customerId = TEST_CUSTOMER_ID;
    _shopParams.isws = @"0";
    _shopParams.merchantId = @"";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(BOOL)isNeedRefresh{
    return true;
}

-(BOOL)isNeedLoadMore{
    return true;
}


-(Class)getClass{
    return [ServiceShopCell class];
}

-(NSString *)getCellID{
    return @"cellID";
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ServiceShopCell * cell = (ServiceShopCell *)[self getCell];
    ShopListEntity * entity = [self getObjectAtIndex:indexPath];
    [MyUtils loadImageWithUrl:entity.imgsrc intoView:cell.logoImage];
    cell.shopName.text = entity.sertitle;
    cell.tvKm.text = [NSString stringWithFormat:@"%@%@",entity.distance,@"km"];
    cell.orderTime.text = [NSString stringWithFormat:@"%@-%@",entity.openTime,entity.closeTime];
    cell.pos = indexPath;
    cell.delegate = self;
    return cell;
}

-(void)onItemClick:(int)tag withPos:(NSIndexPath *)pos{
    if(tag == 1){//点击电话
        ShopListEntity * entity = [self getObjectAtIndex:pos];
        NSMutableString *str2=[[NSMutableString alloc] initWithFormat:@"tel:%@",entity.phone];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]];
        [self.view addSubview:callWebview];
    }else if(tag == 2){//点击预约
        CleanOrderController * orderVc = [[CleanOrderController alloc] init];
        ShopListEntity * entity = [self getObjectAtIndex:pos];
        orderVc.type = 1;
        orderVc.shopId = entity.orgId;
        orderVc.comID = entity.shopId;
        [self.navigationController pushViewController:orderVc animated:YES];
    }else if(tag == 3){//点击导航
        
    }else if(tag == 4){//点击加入收藏
        [_uiView startAnimating];
        ShopListEntity * entity = [self getObjectAtIndex:pos];
        NSDictionary * dct = [[NSDictionary alloc] initWithObjectsAndKeys:TEST_CUSTOMER_ID,@"customerId",entity.orgId,@"companyId", nil];
        [[ApiManager sharedManager] addShopToFav:dct andBlock:^(id data, NSError *error) {
            [_uiView stopAnimating];
            entity.isMy = 1;
            [_dataRyc reloadData];
        }];
    }
}



-(void)getListData{
    [[ApiManager sharedManager]getMyShopList:[_shopParams mj_JSONObject] andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        NSArray * dataArray = [ShopListEntity mj_objectArrayWithKeyValuesArray:resultData];
        [self finishLoadData:dataArray];
    }];
}

-(void)doRefreshData{
    [[ApiManager sharedManager]getMyShopList:[_shopParams mj_JSONObject] andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        NSArray * dataArray = [ShopListEntity mj_objectArrayWithKeyValuesArray:resultData];
        [self finishRefreshData:dataArray];
    }];
}

-(void)doLoadData{
    [[ApiManager sharedManager]getMyShopList:[_shopParams mj_JSONObject] andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        NSArray * dataArray = [ShopListEntity mj_objectArrayWithKeyValuesArray:resultData];
        [self finishLoadMoreData:dataArray];
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopDetailsViewController * detailsVc = [[ShopDetailsViewController alloc] init];
    ShopListEntity * entity = [self getObjectAtIndex:indexPath];
    detailsVc.shopId = entity.shopId;
    [self.navigationController pushViewController:detailsVc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
